package cn.edu.sgu.www.mhxysy.controller.role;

import cn.edu.sgu.www.mhxysy.consts.CustomConstants;
import cn.edu.sgu.www.mhxysy.consts.CustomMessages;
import cn.edu.sgu.www.mhxysy.consts.MimeTypes;
import cn.edu.sgu.www.mhxysy.entity.role.RoleAttribute;
import cn.edu.sgu.www.mhxysy.function.computer.attribute.impl.RoleAttributeComputer;
import cn.edu.sgu.www.mhxysy.restful.JsonResult;
import cn.edu.sgu.www.mhxysy.service.role.RoleAttributeService;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotBlank;
import java.util.Map;

/**
 * @author 沐雨橙风ιε
 * @version 1.0
 */
@Api(tags = "角色属性管理")
@ApiSupport(author = CustomConstants.AUTHOR)
@Validated
@RestController
@RequestMapping(path = "/role_attribute", produces = MimeTypes.APPLICATION_JSON_CHARSET_UTF_8)
public class RoleAttributeController {

	private final RoleAttributeService roleAttributeService;
	private final RoleAttributeComputer roleAttributeComputer;

	@Autowired
	public RoleAttributeController(
			RoleAttributeService roleAttributeService,
			RoleAttributeComputer roleAttributeComputer) {
		this.roleAttributeService = roleAttributeService;
		this.roleAttributeComputer = roleAttributeComputer;
	}

	@ApiOperation("计算角色属性")
	@RequestMapping(value = "/compute", method = RequestMethod.GET)
	public JsonResult<Map<String, Integer>> compute(
			@ApiParam(name = "roleId", value = "角色ID", required = true)
			@NotBlank(message = CustomMessages.ROLE_ID_NOT_NULL) String roleId) {
		Map<String, Integer> compute = roleAttributeComputer.compute(roleId);

		return JsonResult.success(null, compute);
	}

	@ApiOperation("通过ID查询角色属性")
	@RequestMapping(value = "/selectById", method = RequestMethod.GET)
	public JsonResult<RoleAttribute> selectById(
			@ApiParam(name = "id", value = "角色属性ID", required = true)
			@NotBlank(message = CustomMessages.ID_NOT_NULL) String id) {
		RoleAttribute roleAttribute = roleAttributeService.selectById(id);

		return JsonResult.success(null, roleAttribute);
	}

	@ApiOperation("通过角色ID查询角色属性")
	@RequestMapping(value = "/selectByRoleId", method = RequestMethod.GET)
	public JsonResult<RoleAttribute> selectByRoleId(
			@ApiParam(name = "roleId", value = "角色ID", required = true)
			@NotBlank(message = CustomMessages.ROLE_ID_NOT_NULL) String roleId) {
		RoleAttribute roleAttribute = roleAttributeService.selectByRoleId(roleId);

		return JsonResult.success(null, roleAttribute);
	}

	@ApiOperation("通过角色ID和加点ID查询角色属性")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "roleId", value = "角色ID", required = true),
			@ApiImplicitParam(name = "jiadianId", value = "加点ID", required = true)
	})
	@RequestMapping(value = "/selectByRoleIdAndJiadianId", method = RequestMethod.GET)
	public JsonResult<RoleAttribute> select(
			@NotBlank(message = CustomMessages.ROLE_ID_NOT_NULL) String roleId,
			@NotBlank(message = "加点" + CustomMessages.ID_NOT_NULL) String jiadianId) {
		RoleAttribute roleAttribute = roleAttributeService.selectByRoleIdAndJiadianId(roleId, jiadianId);

		return JsonResult.success(null, roleAttribute);
	}

}